'use strict';

angular.module('axonBank')
        .service('BankAccountService', function ($stomp, $q) {

            var isConnected = false;

            return {
                connect: function () {
                    alert("connect in service");
                    return $q(function (resolve, reject) {
                        if (!isConnected) {
                            $stomp.connect('/websocket')
                                    .then(function (frame) {
                                        isConnected = true;
                                        resolve();
                                    })
                                    .catch(function (reason) {
                                        reject(reason);
                                    });
                        } else {
                            resolve();
                        }
                    });
                },
                loadBankAccounts: function () {
                    alert("loadBankAccounts:");
                    return $q(function (resolve, reject) {
                        $stomp.subscribe('/app/bank-accounts', function (data) {
                    alert("loadBankAccounts stomp");
                            resolve(data);
                        });
                    });
                },
                loadBankTransfers: function (bankAccountId) {
                    alert("loadBankTransfers:");
                    return $q(function (resolve, reject) {
                        $stomp.subscribe('/app/bank-accounts/' + bankAccountId + '/bank-transfers', function (data) {
                            resolve(data);
                        });
                    });
                },
                subscribeToBankAccountUpdates: function () {
                    alert("subscribeToBankAccountUpdates:");
                    console.log("subscribeToBankAccountUpdates:");
                    var deferred = $q.defer();
                    console.log(deferred);
                    $stomp.subscribe('/topic/bank-accounts.updates', function (data) {
                        console.log(data);
                        deferred.notify(data);
                    });
                    console.log(deferred);
                    return deferred.promise;
                },

                createBankAccount: function (data) {
                    alert("createBankAccount:");
                    console.log(data);
                    $stomp.send('/app/bank-accounts/create', data);
                },
                deposit: function (data) {
                    alert("deposit:");
                    $stomp.send('/app/bank-accounts/deposit', data);
                },
                withdraw: function (data) {
                    alert("withdraw:");
                    $stomp.send('/app/bank-accounts/withdraw', data);
                },
                transfer: function (data) {
                    alert("transfer:");
                    $stomp.send('/app/bank-transfers/create', data);
                }
            };
        });